﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AuriaCompte.Lib.Dao;
using AuriaCompte.Fenetre;
using AuriaCompte.Lib;

namespace AuriaCompte
{
    public partial class Main : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            kspVertical.Enabled = false;
            LoadConf();
        }

        private void LoadConf()
        {
            this.kMG.GlobalStrings.Abort = Lib.Tools.GetValueFileConfiguration("kMG.Abort");
            this.kMG.GlobalStrings.Cancel = Lib.Tools.GetValueFileConfiguration("kMG.Cancel");
            this.kMG.GlobalStrings.Close = Lib.Tools.GetValueFileConfiguration("kMG.Close");
            this.kMG.GlobalStrings.Ignore = Lib.Tools.GetValueFileConfiguration("kMG.Ignore");
            this.kMG.GlobalStrings.No = Lib.Tools.GetValueFileConfiguration("kMG.No");
            this.kMG.GlobalStrings.OK = Lib.Tools.GetValueFileConfiguration("kMG.OK");
            this.kMG.GlobalStrings.Retry = Lib.Tools.GetValueFileConfiguration("kMG.Retry");
            this.kMG.GlobalStrings.Yes = Lib.Tools.GetValueFileConfiguration("kMG.Yes");
        }

        private void modifierLeMotDePasseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Fenetre.GestionFichierCompteForm gfc = new Fenetre.GestionFichierCompteForm();
            gfc.ShowDialog();
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            opf.ShowDialog();
            Lib.Tools.SetDbFileName(opf.FileName);
            if ((new DaoDb()).Select().IsPasswordProtected)
            {
                MotDePasseFichierDeCompte mddc = new MotDePasseFichierDeCompte();
                if (mddc.Montre())
                {
                    kspVertical.Enabled = true;
                }
                else
                {
                    kspVertical.Enabled = false;
                    Lib.Tools.SetDbFileName(String.Empty);
                }
            }
            else
            {
                kspVertical.Enabled = true;
            }

            if (kspVertical.Enabled)
            {
                this.RempliCbCompte();
                this.RempliCompteSoldePrevi();
            }
        }

        private void RempliCbCompte()
        {
            DaoCompte daoC = new DaoCompte();
            List<Compte> lc = daoC.SelectAll();
            kCbComptes.DataSource = lc;

            kBtnModifieCompte.Enabled = !(lc.Count == 0);
            kBtnSupprimeCompte.Enabled = !(lc.Count == 0);
            kpActionBas.Enabled = !(lc.Count == 0);
            mouvementsToolStripMenuItem.Enabled =  !(lc.Count == 0);
        }

        private void RempliCompteSoldePrevi()
        {
            kDgvCompteSoldePrev.DataSource = (new DaoCompte()).SelectVueSoldePrevi().Tables[0];
        }

        private void kBtnAjoutCompte_Click(object sender, EventArgs e)
        {
            GestionDeCompte gdc = new GestionDeCompte('C');
            gdc.ShowDialog();
            this.RempliCbCompte();
            this.RempliCompteSoldePrevi();
        }

        private void kBtnModifieCompte_Click(object sender, EventArgs e)
        {
            GestionDeCompte gdc = new GestionDeCompte('M', Convert.ToInt32(kCbComptes.SelectedValue));
            gdc.ShowDialog();
            this.RempliCbCompte();
            this.RempliCompteSoldePrevi();
        }

        private void kBtnSupprimeCompte_Click(object sender, EventArgs e)
        {
            if (ComponentFactory.Krypton.Toolkit.KryptonTaskDialog.Show("Erreur", string.Empty, "Etes-vous sur de supprimer ce compte : \"" + this.kCbComptes.Text + "\" ?", MessageBoxIcon.Question, ComponentFactory.Krypton.Toolkit.TaskDialogButtons.Yes | ComponentFactory.Krypton.Toolkit.TaskDialogButtons.No) == System.Windows.Forms.DialogResult.Yes)
            {
                (new DaoCompte()).Delete(Convert.ToInt32(kCbComptes.SelectedValue));
                this.RempliCbCompte();
                this.RempliCompteSoldePrevi();
            }
        }
    }
}
